<?php
/**
 * Created by PhpStorm.
 * User: Office
 * Date: 2017/12/13
 * Time: 16:27
 */
header("Content-type: text/html; charset=utf-8");
$login = false;

session_start();

if (isset($_SESSION['user']) && $_SESSION['user'] !== '')
    $login = true;

$file_arr = array();
//分类文件名
$class_arr = array();
//文件里的日志
$folder_arr = array();
$post_data = array();
//此目录下的文件名
//$dir =  dirname(__FILE__).'/../diary';
$dir =  '../diary';

//打开此文件夹

$dh = @opendir($dir);
if ($dh) {
    while ($filename = readdir($dh)) {
        if ($filename !== '.' && $filename !== '..') {
            array_push($class_arr,$filename);
            $dirson = opendir($dir . '/' . $filename);
            if ($dirson) {
                while ($fileson = readdir($dirson)) {
                    if ($fileson !== '.' && $fileson !== '..') {
                        array_push($folder_arr, $fileson);
                    }
                }
            }
        }
    }


//    获取文件夹下文件名称

}
rsort($class_arr);
rsort($folder_arr);
//exit();

foreach ($folder_arr as $fileName) {
    $file_name = $dir . '/' . substr($fileName, 0, 6) . '/' . $fileName;
    if (file_exists($file_name)) {
//        只读方式打开文件
        $fp = fopen($file_name, 'r');
        if ($fp) {
//            上锁
            flock($fp, LOCK_SH);
//            获取文件的已写入内容
            $result = fread($fp, filesize($file_name));
        }
        flock($fp, LOCK_UN);
        fclose($fp);
    }
    $temp_data = array();
//    获得内容以'|'形式储存为数组
    $content_array = explode('|', $result);
    $temp_data['SUBJECT'] = $content_array[0];                //文章标题
    $temp_data['DATE'] = substr($content_array[1], 0, 4) . '-' . substr($content_array[1], 4, 2) . '-' . substr($content_array[1], 6, 2);                   //文章日期
    $temp_data['CONTENT'] = $content_array[2];               //文章内容
    $temp_data['FILENAME'] = $dir . '/' . substr($fileName, 0, 6) . '/' . $fileName;            //文章目录
    array_push($post_data, $temp_data);

}
//closedir($dh);
//closedir($dirson);
//echo "<pre>";
//print_r($temp_data);
//echo "</pre>";
//echo '$post_data';
//echo "<pre>";
//print_r($post_data);
//echo "</pre>";


?>